package controller;

import com.alibaba.fastjson.JSONArray;
import com.mysql.jdbc.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.Book;
import service.BookService;
import tools.Page;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.logging.Logger;

/**
 * Created by Administrator on 2019/1/4.
 */
@Controller
@RequestMapping("/book")
public class BookController {

    @Resource
    BookService bookService;


    @RequestMapping("/list")
    public String getBookList(HttpServletRequest request, @RequestParam(value="chooseId",required=false)Integer chooseId,@RequestParam(value="choose",required=false)String choose, Model model){
    String pageIndex=request.getParameter("pageIndex");
        try {
            int currPageNo = 1;

            if (pageIndex == "" || pageIndex == null) {
                currPageNo = 1;
            } else {
                currPageNo = Integer.parseInt(pageIndex);
            }
            Page pageObj = new Page();
            pageObj.setCurrPageNo(currPageNo);
            pageObj.setPageSize(2);
            pageObj = bookService.getBookList(chooseId,choose,pageObj);
            model.addAttribute("page",pageObj);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "index";

    }

    @RequestMapping("/add")
    public String add(Book book,HttpServletResponse response){
        try {
            PrintWriter out = response.getWriter();
            int result = bookService.add(book);
            if (result > 0) {
                out.print("<script type=\"text/javascript\">");
                out.print("alert(\"添加成功\");");
                out.print("</script>");
            }else{
                out.print("<script type=\"text/javascript\">");
                out.print("alert(\"添加失败\");");
                out.print("</script>");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:/book/UserByid";

    }
    @RequestMapping("/addBook")
    public String add(){

        return "addBook";

    }



//    @RequestMapping(value = "/UserB.yid")
//    public String gitUserByid(Book book,HttpServletResponse response){
//        try {
//            PrintWriter out = response.getWriter();
//            int result = bookService.add(book);
//            if (result > 0) {
//                out.print("<script type=\"text/javascript\">");
//                out.print("alert(\"修改成功\");");
//                out.print("</script>");
//            }else{
//                out.print("<script type=\"text/javascript\">");
//                out.print("alert(\"修改失败\");");
//                out.print("</script>");
//            }
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return "redirect:/book/list";
//
//    }

    @RequestMapping(value = "/view{yid}")
    public String gitUserByid(Book book,HttpServletResponse response){
        try {
            PrintWriter out = response.getWriter();
            int result = bookService.add(book);
            if (result > 0) {
                out.print("<script type=\"text/javascript\">");
                out.print("alert(\"修改成功\");");
                out.print("</script>");
            }else{
                out.print("<script type=\"text/javascript\">");
                out.print("alert(\"修改失败\");");
                out.print("</script>");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:/book/list";

    }
    @RequestMapping(value="/ucexist.html")
@ResponseBody
    public  Object uderCodeIsExit(@RequestParam String userCode){
        Logger.debug("uderCodeIsExit userCode:"+userCode);
        HashMap<String,String> resultMap=new HashMap<String,String>();
        if(StringUtils.isNullOrEmpty(userCode)){

        }else {
            Book book=BookService.selectUserCodeExist(userCode);
            if(null !=book){
                resultMap.put("userCode","exist");


            }else{

                resultMap.put("userCode","noexist");

            }
            return JSONArray.toJSONString(resultMap);
        }


    }



}
